<template>
  <div class="goods">

     <!-- 1.目录位置 -->
    <div class="nav">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 搜索区域 -->
    <div class="header">
      <!-- change	仅在输入框失去焦点或用户按下回车时触发 -->
      <el-input
        v-model="input"
        placeholder="请输入内容"
        @change="searchInput"
      ></el-input>
      <el-button type="primary">查询</el-button>
      <el-button type="primary" @click="addGoods">添加</el-button>
    </div>
    <!-- 表格区域展示视图数据 -->
    <div class="wapper">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="商品ID" width="100"></el-table-column>
       <el-table-column
          prop="title"
          label="商品名称"
          width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="price"
          label="商品价格"
          width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="num"
          label="商品数量"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="image"
          label="商品图片"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="sellPoint"
          label="商品卖点"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="descs"
          label="商品描述"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="category"
          label="商品类目"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              ><i class="el-icon-edit"></i>编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              ><i class="el-icon-delete"></i>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <Pagination :total="total" :pageSize="pageSize" @changePage="changePage" />
    <!-- 弹框 -->
    <!-- <GoodsDialog :dialogVisible="dialogVisible" @changeDialog="changeDialog"/> -->
    <GoodsDialog ref="dialog" :title="title" :rowData="rowData" />
  </div>
</template>

<script>
import Pagination from "../../components/Pagination.vue";
import GoodsDialog from "./GoodsDialog.vue";
export default {
  name: "Goods",
  components: {
    Pagination,
    GoodsDialog,
  },
  data() {
    return {
      input: "",
      tableData: [],
      total: 10,
      pageSize: 1,
      type: 1,
      list: [], //获取搜索总数据的条数
      dialogVisible: false,
      title: "添加商品",
      rowData: {}, //当前行的数据信息
    };
  },
  methods: {
    // 弹框添加
    addGoods() {
      // this.dialogVisible =true
      this.$refs.dialog.open();
      this.title = "添加商品";
    },
    changeDialog() {
      this.dialogVisible = false;
    },
    //分页页码
    changePage(num) {
      if ((this.type = 1)) {
        this.getHttp(num); //商品列表分页
      } else {
        //搜索分页
        console.log("搜索的分页处理----");
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    //搜索查询数据
    searchInput(value) {
    //  console.log(value);
     if (!value) {
        this.getHttp(1);
        return;
      } // console.log('搜索',value);
      else {
        this.$api
          .getSearch({
            search: value,
          })
          .then((res) => {
            console.log("搜索----", res.data); //根据商品的名称，卖点，描述去搜索
            if (res.data.status === 200) {
              this.list = res.data.result; //获取的搜索总数据条数 -- 数据分割
              //假设没有分页 ， 需要处理分页
              this.total = res.data.result.length;
              this.pageSize = 3;
              this.tableData = res.data.result.slice(0, 3);
              this.type = 2;
            } else {
              this.tableData = [];
              this.total = 1;
              this.pageSize = 1;
              this.type = 1;
            }
          });
      }
    },
    // 编辑操作
    handleEdit(index, row) {
      //1.点击编辑得按钮,显示弹框 2.弹框上显示上回得数据展示 - 当前得行得数据
      this.$refs.dialog.dialogVisible = true;
      this.title = "编辑商品";
      this.rowData = {...row};
      // this.$refs.dialog.goodsForm = row;第一种方法
    },
    //删除操作
    handleDelete(index, row) {
      //index下标  row是当前行的数据内容{title:'',price:'',...}
      this.$confirm("您将永久删除该该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .getDeleteGoods({
              id: row.id,
            })
            .then((res) => {
              console.log(res.data);
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                //视图更新
                this.getHttp(1);
              } else {
                this.$message({ type: "info", message: "已取消删除" });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //商品列表的获取
    getHttp(page) {
      this.$api
        .getGoodsList({
          page,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.tableData = res.data.data; //数据列表
            this.total = res.data.total; //总页数
            this.pageSize = res.data.pageSize;
          }
        });
    },
  },
  //生命周期函数
  mounted() {
    this.getHttp(1);
  },
};
</script>

<style lang='less' scoped>
.nav {
  padding: 10px;
}
.goods {
  height: 100%;
  margin: 20px;
}
.header {
  height: 70px;
  display: flex;
  align-items: center;
  .el-button {
    margin-left: 20px;
  }
}
.wapper {
  margin: 20px 0px;
}
</style>